@extends('admin.layouts.head')

@section('content')
    <body>
    <div id="app">
        <div class="wrapper -header-fixed">

        @include('admin.layouts.navbar')
        @include('admin.layouts.sidebar')

        <!-- 内容页开始-->
            <div class="content-container">
                <div class="container-fluid">
                    <div class="page-content">

                        <nav aria-label="breadcrumb">
                            <ol class="breadcrumb">
                                <li class="breadcrumb-item"><a href="{{ route('admin.dashboard') }}">仪表盘</a></li>
                                <li class="breadcrumb-item active" aria-current="page">管理员</li>
                            </ol>
                        </nav>
                        @verbatim
                        <div class="card">
                            <div class="card-body">

                                <div id="vueApp">
                                    <div class="content-box">

                                        <div class="op-bar" style="margin: 16px 0;">
                                            <el-button type="primary" @click="createAdmin" size="mini">添加</el-button>
                                        </div>

                                        <el-table
                                            :data="adminList"
                                            border
                                            size="mini"
                                            style="width: 100%">
                                            <el-table-column
                                                prop="id"
                                                label="ID"
                                                width="80"
                                            >
                                            </el-table-column>
                                            <el-table-column
                                                prop="username"
                                                label="账号"
                                            >
                                            </el-table-column>
                                            <el-table-column
                                                label="角色"
                                                width="100"
                                            >
                                                <template slot-scope="scope">
                                                    <span v-if="scope.row.role === 0">超管</span>
                                                    <span v-if="scope.row.role === 1">普通</span>
                                                    <span v-else>客服</span>
                                                </template>
                                            </el-table-column>

                                            <el-table-column
                                                label="操作"
                                                width="180"
                                            >
                                                <template slot-scope="scope">
                                                    <el-button @click="editAdmin(scope.row)" type="primary" round plain size="small">编辑</el-button>
                                                    <el-button type="danger" round plain size="small" @click="delAdmin(scope.row.id)">删除</el-button>
                                                </template>
                                            </el-table-column>
                                        </el-table>


                                        <!-- 保存窗口 -->
                                        <el-dialog
                                            title="保存管理员"
                                            :visible.sync="saveShow"
                                            width="600"
                                            >
                                            <div class="save-win" style="margin-right: 30px;">
                                                <el-form ref="saveForm" :model="saveForm" label-width="100px">
                                                    <el-form-item label="账号">
                                                        <el-input v-model="saveForm.username"></el-input>
                                                    </el-form-item>
                                                    <el-form-item label="密码" v-if="!saveForm.id">
                                                        <el-input v-model="saveForm.password"></el-input>
                                                    </el-form-item>
                                                    <el-form-item label="角色">
                                                        <el-select v-model="saveForm.role" placeholder="请选择">
                                                            <el-option label="超管" :value="0"></el-option>
                                                            <el-option label="普通" :value="1"></el-option>
                                                            <el-option label="客服" :value="2"></el-option>
                                                        </el-select>
                                                    </el-form-item>
                                                    <el-form-item>
                                                        <el-button type="primary" @click="saveAdmin">保存</el-button>
                                                        <el-button @click="saveShow=false">取消</el-button>
                                                    </el-form-item>
                                                </el-form>
                                            </div>
                                        </el-dialog>

                                    </div>

                            </div> <!-- .card-body -->
                        </div> <!-- .card -->
                        @endverbatim
                    </div> <!-- .page-content -->
                </div> <!-- .container-fluid -->
            </div> <!-- .content-container -->
        </div> <!-- .wrapper -->
    </div> <!-- #app -->

    @include('admin.layouts.vuehead')

    <script>
        const vueApp = new Vue({
            el: '#vueApp',
            data() {
                return {
                    adminList: [],
                    saveShow: false,
                    saveForm: {
                        id: 0,
                        username: '',
                        password: '',
                        role: 0,
                    }
                }
            },
            mounted() {
                this.getAdminList()
            },
            watch: {

            },
            methods: {
                getAdminList() {
                    adminApi_getAdmin().then(res => {
                        this.adminList = res.data
                    })
                },
                saveAdmin() {
                    adminApi_saveAdmin(this.saveForm).then(res => {
                        this.$message.success('保存成功')
                        this.saveShow = false
                        this.getAdminList()
                    }).catch(err => {
                        this.$message.error(err.message)
                    })
                },
                editAdmin(admin) {
                    this.saveForm = admin
                    this.saveShow = true
                },
                createAdmin() {
                    this.saveForm = {
                        id: 0,
                        username: '',
                        password: '',
                        role: 0,
                    }
                    this.saveShow = true
                },
                delAdmin(id) {
                    this.$confirm('确认删除吗?', '提示', {
                        confirmButtonText: '确定',
                        cancelButtonText: '取消',
                        type: 'warning'
                    }).then(() => {
                        adminApi_delAdmin({id}).then(res => {
                            this.$message.success('删除成功')
                            this.getAdminList()
                        })
                    }).catch(() => {});

                }
            },
        })
    </script>
    </body>



@endsection
